#include <stdio.h>
#include <memory.h>

bool sheng[ 1000 ][ 1000 ];
int n;

void unionset( int a, int b )
{
    int i;
    sheng[ a ][ b ] = true;
    for ( i = 0; i < n; i++ )
        if ( sheng[ b ][ i ] && !sheng[ a ][ i ] && a != i )
            unionset( a, i );
    for ( i = 0; i < n; i++ )
        if ( sheng[ i ][ a ] && !sheng[ i ][ b ] && b != i )
            unionset( i, b );
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        int m, a, b, i, j, count = 0;
        scanf("%d%d", &n, &m);
        memset( sheng, 0, sizeof( sheng ) );
        while ( m-- )
        {
            scanf("%d%d", &a, &b);
            unionset( a - 1, b - 1 );
        }
        for ( i = 0; i < n; i++ )
        {
            for ( j = 0; j < n; j++ )
            {
                if ( i == j ) continue;
                if ( sheng[ i ][ j ] ) count++;
            }
        }
        printf("%d\n", ( n * n - count * 2 - n ) / 2);
    }
    return 0;
}
